/*====================================================================================
                                                                     
		Financial Constraints and Propagation of Shocks in Production Networks
			    By Demir-Javorcik-Michalski-Ors
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
Figure II.1
***************************************/

use "tempyvar",clear

keep if year==2010 |  year==2011 | year==2012

bys HS6 country_code,sort: gen numobs=_N
keep if numobs==3 //Keep continuing varieties

bys year,sort : su RUSFShr_vy,det


tw (kdensity RUSFShr_vy if year==2010,lcolor(black) lpattern(dot)) (kdensity RUSFShr_vy if year==2011,lpattern(dash) lcolor(black)) (kdensity RUSFShr_vy if year==2012, lcolor(black) lpattern(solid)) ,ytitle("Density") xtitle("Exposure{subscript:t}") legend(on order(1 2 3) label(1 "t=2010") label(2 "t=2011") label(3 "t=2012") row(1)) graphregion(color(white)) 
graph export "Density_w0s.eps",replace

tw (kdensity RUSFShr_vy if year==2010 & RUSFShr_vy>0,lcolor(black) lpattern(dot)) (kdensity RUSFShr_vy if year==2011 & RUSFShr_vy>0,lpattern(dash) lcolor(black)) (kdensity RUSFShr_vy if year==2012 & RUSFShr_vy>0, lcolor(black) lpattern(solid)) ,ytitle("Density") xtitle("Exposure{subscript:t}") legend(on order(1 2 3) label(1 "t=2010") label(2 "t=2011") label(3 "t=2012") row(1)) graphregion(color(white)) 
graph export "Density_wo0s.eps",replace


/***************************************
Figure II.3
***************************************/
use "tempyvar",clear
drop if year>2011

egen py=group(HS6 year)
egen cy=group(country_code year)
egen cp=group(HS6 country_code)

reghdfe RUSFShr_vy,absorb(py cy fe=cp)

keep if year==2010
tw lpoly RUSFShr_vy fe,lcolor(black) graphregion(color(white)) xtitle(Estimated variety (v) fixed effects) xtitle("Estimated variety (v) fixed effects") ytitle("Exposure{subscript:v,t=2010}")
graph export "Stability.eps",replace



/***************************************
Figure II.4
***************************************/

use tempfirmexp,clear

sort firm_ID
merge firm_ID using tempbartik
drop _m


twoway (hist FirmExposure , frac lcolor(gs12) fcolor(gs12)) (hist BartikExposure , frac fcolor(none) lcolor(black)), ylabel(0(0.2)0.8) legend(on order(1 2) label(1 "Exposure{superscript:Actual}") label(2 "Exposure")) ytitle("Fraction") graphregion(color(white))

twoway (hist FirmExposure if FirmExposure>0, frac lcolor(gs12) fcolor(gs12)) (hist BartikExposure if FirmExposure>0 , frac fcolor(none) lcolor(black)), ylabel(0(0.2)0.8) legend(on order(1 2) label(1 "Exposure{superscript:Actual}") label(2 "Exposure")) ytitle("Fraction") graphregion(color(white))


/***************************************
Figure II.2
***************************************/
use "data/Google.dta",clear

gen num=_n

label define date 1 "2009-01" 12 "2009-12" 23 "2010-12" 29 "2011-10"  43 "2012-12"
label values num date

tw line google num,  lcolor(black) xlabel(1 12 23 29 43 ,valuelabels angle(90)) xline(29,lcolor(black)) graphregion(color(white)) ytitle("") ytitle("") xtitle("") legend(off)


/***************************************
Table II.8
***************************************/

use "tempyvar",clear

keep if year==2010 

*Within product (upper panel)
reshape wide RUSFShr_vy,i(HS6) j(country_code) string
egen avg_p=rowmean(ExposureAGO-ExposureZWE)
egen avg=mean(avg_p)
br HS6 Exposure* if avg_p<avg // Table lists examples of countries with low-exposure within a given product

br HS6 Exposure* if avg_p>avg // Table lists examples of countries with high-exposure within a given product

drop avg*
reshape long RUSFShr_vy,i(HS6) j(country_code) string


*Within source country (lower panel)
reshape wide RUSFShr_vy,i(country_code) j(HS6) 
egen avg_c=rowmean(Exposure100110-Exposure983100)
egen avg=mean(avg_c)
br country_code Exposure* if avg_c<avg // Table lists examples of products with low-exposure for a given source country
br country_code Exposure* if avg_c>avg // Table lists examples of products with high-exposure for a given source country
